<template>
	<view class="padding-30">
		<view class="padding-30 bg-fff border-radius20 margin-bottom30">
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">药品名称</view>
				<input type="text" v-model="medicine_name" placeholder="请输入" class="input-item"/>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30" 
			@click="$tools.navigateTo('/pages/mine/mine-list/health-records/drug-dosage')">
				<view class="fontSize-15 color-000 margin-right20">药品用量</view>
				<view class="flex-alignItems-center">
					<view class="fontSize-15 color-000 margin-right10" v-if="medicine_dosage">{{medicine_dosage}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">用药频率</view>
				<input type="text" v-model="medicine_frequency" placeholder="请输入" class="input-item"/>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">用药开始时间</view>
				<view class="flex-alignItems-center" @click="start_dateDateShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="start_dateName">{{start_dateName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
			<view class="flex-between padding-bottom30 f-divider1 margin-bottom30">
				<view class="fontSize-15 color-000 margin-right20">用药结束时间</view>
				<view class="flex-alignItems-center" @click="end_dateDateShow = true">
					<view class="fontSize-15 color-000 margin-right10" v-if="end_dateName">{{end_dateName}}</view>
					<view class="fontSize-15 color-999 margin-right10" v-else>请选择</view>
					<u-icon name="arrow-right" color="#666666" size="18"></u-icon>
				</view>
			</view>
		</view>
		
		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="position-fixed-bottom padding-30 bg-fff">
			<view class="btn50-0165FD-15ffffff flex-center" @click="SubmitEvent">
				保存
			</view>
		</view>
		
		<!-- 用药开始时间 -->
		<u-datetime-picker  :maxDate="Number(new Date())" :show="start_dateDateShow" v-model="start_dateValue" mode="datetime" 
		@cancel="start_dateDateCancel" @confirm="start_dateDateConfirm"></u-datetime-picker>
		<!-- 用药结束时间 -->
		<u-datetime-picker  :maxDate="Number(new Date())" :show="end_dateDateShow" v-model="end_dateDateValue" mode="datetime" 
		@cancel="end_dateCancel" @confirm="end_dateConfirm"></u-datetime-picker>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data(){
			return{
				medicine_name:'', // 药品名称
				medicine_dosage:'' ,// 药品用量
				medicine_frequency:'', // 用药频率
				medicine_start_date:'', // 用药开始时间
				medicine_end_date:'', // 用药结束时间
				// 用药开始时间
				start_dateDateShow:false,
				start_dateValue:Number(new Date()),
				start_dateName:'',
				// 用药结束时间
				end_dateDateShow:false,
				end_dateDateValue:Number(new Date()),
				end_dateName:'',
				id:''
			}
		},
		onLoad() {
			if(e.id){
				if(uni.getStorageSync('patlient-item')){
					this.sick_id = uni.getStorageSync('patlient-item').id
				}
				this.id = e.id
				this.getDetail()
			}
			if(uni.getStorageSync('patlient-item')){
				this.sick_id = uni.getStorageSync('patlient-item').id
			}
		},
		onShow() {
			this.getdrugDosage()
			if(uni.getStorageSync('patlient-item')){
				this.sick_id = uni.getStorageSync('patlient-item').id
			}
		},
		onHide() {
			this.deleteformItem()
		},
		methods:{
			...mapMutations(['deleteformItem']),
			// 获取详情
			async getDetail(){
				let res = await this.$http.post(this.$api.medicine_detail, {
					id:this.id,
					sick_id: this.sick_id
				}, {
					token: true
				})
				if (res.code == 1) {
					this.medicine_name = res.data.medicine_name
					this.medicine_dosage = res.data.medicine_dosage
					this.medicine_frequency = res.data.medicine_frequency
					this.start_dateName = res.data.medicine_start_date
					this.end_dateName = res.data.medicine_end_date
				}
			},
			// 提交
			async SubmitEvent(){
				if(!this.medicine_name)return this.$tools.toast('请填写药品名称')
				if(!this.medicine_dosage)return this.$tools.toast('请选择药品用量')
				if(!this.medicine_frequency)return this.$tools.toast('请填写用药频率')
				if(!this.start_dateName)return this.$tools.toast('请选择用药开始时间')
				if(!this.end_dateName)return this.$tools.toast('请选择用药结束时间')
				let form = {
					sick_id:this.sick_id,
					medicine_name:this.medicine_name,
					medicine_dosage:this.medicine_dosage,
					medicine_frequency:this.medicine_frequency,
					medicine_start_date:this.start_dateName,
					medicine_end_date:this.end_dateName
				}
				if(this.id){
					form.id = this.id
					let res = await this.$http.post(this.$api.medicine_edit,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('修改成功')
						this.$tools.navigateBack(1,200)
					}
				}else{
					let res = await this.$http.post(this.$api.medicine_add,form,{token:true})
					if(res.code ==1){
						this.$tools.toast('添加成功')
						this.$tools.navigateBack(1,200)
					}
				}
			},
			// 确认结束时间选择
			end_dateConfirm(e){
				this.end_dateName = this.$T.timeFormat(e.value)
				this.end_dateDateShow = false
			},
			// 取消结束时间选择
			end_dateCancel(){
				this.end_dateName = ''
				this.end_dateDateShow = false
			},
			// 确认开始时间选择
			start_dateDateConfirm(e){
				this.start_dateName = this.$T.timeFormat(e.value)
				this.start_dateDateShow = false
			},
			// 取消开始时间选择
			start_dateDateCancel(){
				this.start_dateName = ''
				this.start_dateDateShow = false
			},
			// 药品用量
			getdrugDosage(){
				let item = {}
				let values = ''
				item = this.$store.state.qualifications.form
				if(item.koufu){
					values = values+ item.koufu + '/'
				}
				if(item.yaopin){
					values = values+ item.yaopin + '/'
				}
				if(item.yongliangdanwei){
					values = values+ item.yongliangdanwei + '/'
				}
				if(item.yaopinyongliang){
					values = values+ item.yaopinyongliang + '/'
				}
				this.medicine_dosage = values
			}
		}
	}
</script>

<style lang="scss" scoped>
	
</style>